﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;

namespace formworks.DBO.Core
{
    public class DBConfigure
    {
        protected string connectionString;
        protected SqlConnection connection;
        /// <summary>
        /// 数据库类型
        /// </summary>
        public enum DBType
        {
            MSSQL,
            Oracle,
            Mysql,
            DB2,
            SyBase
        }

        public DBType dbType;
        /// <summary>
        /// 数据库版本号
        /// </summary>
        public string DBVersion;

        public DBConfigure()
        {
            try
            {

                connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["DBServer"].ConnectionString;
                DBVersion = System.Configuration.ConfigurationManager.ConnectionStrings["DBServer"].ProviderName;
            }
            catch (System.Exception ex)
            {
                throw new Exception("web.config数据库配置文件指定DBServer数据连接不存在，或设置错误！");	
            }
        }

        public DBConfigure(string connectionString)
        {
            this.connectionString = connectionString;
        }

        //public DBConfigure(string connectionStringsName)
        //{
        //    connectionString = System.Configuration.ConfigurationManager.ConnectionStrings[connectionStringsName].ConnectionString;
        //}

        

        public DBSessionFactory buildSessionFactory()
        {
            try
            {

                //读取配置文件

                //根据配置文件初始化到一个DBSessionFactory里面

                //获取数据库类型
                dbType = DBType.MSSQL;

                //对话工厂接口
                DBSessionFactory dbSessionFactory = null;
                switch (dbType)
                {
                    //初始化微软数据库
                    case DBType.MSSQL:
                        if (null == connection)
                            dbSessionFactory = new DBMSSQL(connectionString);
                        else
                            dbSessionFactory = new DBMSSQL(connection);
                        //数据库版本
                        dbSessionFactory.DBVersion = DBVersion;
                        break;
                    //初始化甲骨文数据库
                    case DBType.Oracle:
                        //if (null == connection)
                        //    dbSessionFactory = new DBOracle(connectionString);
                        //else
                        //    dbSessionFactory = new DBOracle(connection);
                        break;
                    //初始化sun mysql 数据库
                    case DBType.Mysql:
                        //if (null == connection)
                        //    dbSessionFactory = new DBMysql(connectionString);
                        //else
                        //    dbSessionFactory = new DBMysql(connection);
                        break;
                    default: break;
                }
                //返回此数据库实例
                return dbSessionFactory;
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }//end builder

    }//end class
}//end namespace
